<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<script type="text/javascript">
var dataDictItemDataGrid = $('#datadict-item-datagrid-json').datagrid({
    gridTitle : '数据项管理',
    showLinenumber: false,
    showToolbar: true,
    filterThead: false,
    filterAll: true,
    sortAll : true,
    dataUrl: 'dataDict/item/list.do?dataTypeItem=${dataTypeItem}',
    dataType: 'json',
    columns: [
        {
            name: 'dataShowName',
            label: '数据名称',
            align: 'center',
            width: 150
        },
        {
            name: 'dataValue',
            label: '数据值',
            align: 'center',
            width: 150
        },
        {
            name: 'mark',
            label: '描述',
            align: 'center',
            width: 300,
            render: function(value){
            	if(value){
            		return value;
            	}else{
            		return "";
            	}
            }
        },
        {
			name: '',
			label: '操作',
			align: 'center',
			width: 150,
			quicksort:false,
			menu: false,
			edit: false,
			render: function(value){
		        var html = '';
		    		html += '<div class="btn-group" datadict="group"><button type="button" class="btn btn-blue btn-sm" onclick="editDataDictItem(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
		    		html += '<div class="btn-group" datadict="group"><button type="button" class="btn btn-red btn-sm" onclick="delDataDictItem(this)"><i class="fa fa-remove"></i> 删除</button></div>';
		    	return html;
			}
        }
    ],
    paging: {pageSize:20, selectPageSize:'20,30,40'},
    showTfoot: true,
    fullGrid: false,
    toolbarCustom: function(){
    	var html = '';
    		html += '<div class="btn-group" datadict="group"><button type="button" class="btn btn-blue" onclick="addDataDictItem()"><i class="fa fa-plus"></i> 添加</button></div>';
	    	html += '<div class="btn-group" datadict="group"><button type="button" class="btn btn-green" onclick="refreshdataDictItemDataGrid()"><i class="fa fa-refresh"></i> 刷新</button></div>';
	    return html;
    }
});

function addDataDictItem() {
	$('#edit-datadict-item-dialog').dialog({
		id:'add-datadict-item-dialog', 
		url:'dataDict/item/add.do?dataType=${dataTypeItem}&dataShowType=${dataShowTypeItem}', 
		maxable: false,
		resizable:false,
		mask: true,
		width: 850,
		height: 250,
		onLoad: function($dialog){
			setEditDataDictItemBtnEvent($dialog, true,"add-datadict-item-dialog");
		},
		title:'添加数据项'
	});
}

function editDataDictItem(_this) {
	var datadictIndex = $(_this).closest("tr").index();
	var data = dataDictItemDataGrid.data('bjui.datagrid').getRowData(datadictIndex);
	$('#edit-datadict-item-dialog').dialog({
		id:'edit-datadict-item-dialog', 
		url:'dataDict/item/edit.do', 
		maxable: false,
		resizable:false,
		mask: true,
		width: 850,
		height: 250,
		onLoad: function($dialog){
			setEditDataDictItemBtnEvent($dialog, true, "edit-datadict-item-dialog");
			getJsonData2Form('dataDict/item/getDataDictItem.do', 'dataDictSn='+data.dataDictSn, $('#datadict_item_form'), null);
		},
		title:'编辑数据项'
	});
}

function delDataDictItem(_this) {
	var datadictIndex = $(_this).closest("tr").index();
	var rowData = dataDictItemDataGrid.data('bjui.datagrid').getRowData(datadictIndex);
	dataDictItemDataGrid.alertmsg('confirm', '确定删除当前数据？', {
		okName:'确定', 
		cancelName:'取消', 
		title:'提示信息',
		okCall : function(){
			var url = "dataDict/item/del.do?dataType=${dataTypeItem}&dataDictSn="+rowData.dataDictSn;
			var data = null;
			var successCallBack = function(result){
				refreshdataDictItemDataGrid();
			}
			var failCallBack = null;
			var errorCallBack = null;
			myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
       	}
	});
}

function setEditDataDictItemBtnEvent($dialog, isAdd, trIdx) {
	var $save, $cancel;
	$save = $dialog.find('.save');
	$cancel = $dialog.find('.cancel');

	$save.click(function(){
		$('#datadict_item_form').isValid(function(v) {
			if(v){
				var url = 'dataDict/item/save.do';
				var data = $('#datadict_item_form').serialize()+"&addFlag="+isAdd;
				var successCallBack = function(result){
					$dialog.dialog('close',trIdx);
					refreshdataDictItemDataGrid();
				}
				var failCallBack = null;
				var errorCallBack = null;
				myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
			}
		});
	});

	$cancel.click(function(){
		$dialog.dialog('close',trIdx);
	});
}

function refreshdataDictItemDataGrid() {
	dataDictItemDataGrid.datagrid("refresh");
}
</script>
<div class="bjui-pageContent" id="datadict-item-index-pagecontent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="datadict-item-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div class="bjui-pageFooter">
	<ul>
		<li><button type="button" class="btn btn-red cancel" data-icon="remove">关闭</button></li>
	</ul>
</div>
<div id="edit-datadict-item-dialog" style="display: none;">
</div>
